<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="产品库名称" prop="productlibraryName" style="width:250px">
        <el-input v-model="queryParams.productlibraryName" placeholder="请输入产品库名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" style="width:250px">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否有效" prop="isStatus" style="width:250px">
        <el-select v-model="queryParams.isStatus" placeholder="请选择是否有效" clearable>
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="small" icon="Plus" @click="handleAdd" v-hasPermi="['manage:library:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" size="small" icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:library:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" size="small" icon="Download" @click="handleExport"
          v-hasPermi="['manage:library:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="libraryList" @selection-change="handleSelectionChange" @row-click="(row)=>{console.log(row)}">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" type="index" width="60" align="center"/>
      <el-table-column label="名称" align="center" prop="productlibraryName" />
      <el-table-column label="状态" align="center" prop="isStatus">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.isStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="排序" align="center" prop="sort" sortable/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
			<el-link icon="Edit" class="table_link_btn" type="primary"  :underline="false" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:library:edit']"><span class="table_link_text">修改</span></el-link>
          <el-link icon="Delete" class="table_link_btn" type="danger"  :underline="false" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:library:remove']"><span class="table_link_text">删除</span></el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品库管理对话框 -->
    <el-dialog :title="title" v-model="open" width="30%" append-to-body>
      <el-form ref="libraryRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品库名称" prop="productlibraryName">
          <el-input :row="11" v-model="form.productlibraryName" placeholder="请输入产品库名称" />
        </el-form-item>
        <el-form-item label="产品库排序" prop="sort">
			<el-input-number v-model="form.sort" controls-position="right" :min="0" />
		</el-form-item>
        <el-form-item label="产品库状态" prop="isStatus">
          <el-radio-group v-model="form.isStatus">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Library" lang="ts">
import { listLibrary, delLibrary, addLibrary, updateLibrary,qyListLibrary } from '@/api/manage/library';
import { getCurrentInstance, ref } from 'vue';
import useUserStore from '@/store/modules/user';
const { proxy } = getCurrentInstance() as any;
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<number[]>([]);
const names = ref<string[]>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
const libraryList = ref([]);
const { sys_normal_disable } = proxy.useDict('sys_normal_disable');
const queryRef = ref();
const {roles} = useUserStore();

const form = ref<any>({});
const queryParams = ref({
	pageNum: 1,
	pageSize: 10,
	productlibraryName: '',
	createBy: '',
	isStatus:''
});
const rules = ref({
	productlibraryName: [
		{
			required: true,
			message: '产品库名称不能为空',
			trigger: 'blur',
		},
	]
});
/** 查询产品库管理列表 */
function getList() {
	loading.value = true;
	console.log('roles:',roles);
	if(roles.includes('areaAdmin')){
		qyListLibrary(queryParams.value).then((response: any) => {
			libraryList.value = response.data.list;
			total.value = response.data.total;
			loading.value = false;
		});
		return;
	}
	listLibrary(queryParams.value).then((response: any) => {
		libraryList.value = response.data.list;
		total.value = response.data.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		productlibraryName: null, sort: 0, isStatus: '0', createBy: '', 
	};
	proxy.resetForm('libraryRef');
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm(queryRef);
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection: any) {
	ids.value = selection.map((item: any) => item.id);
	names.value = selection.map((item: any) => item.productlibraryName);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = '添加产品库管理';
}

/** 修改按钮操作 */
function handleUpdate(row: any) {
	reset();
	form.value = Object.assign({},row);
	open.value = true;
	title.value = '修改产品库管理';
	// const id = row.id || ids.value;
	// getLibrary(id).then(response => {
	// 	form.value = response.data;
	// 	open.value = true;
	// 	title.value = '修改产品库管理';
	// });
}

/** 提交按钮 */
function submitForm() {
	proxy.$refs['libraryRef'].validate((valid: boolean) => {
		if (valid) {
			if (form.value.id != null) {
				updateLibrary(form.value).then(response => {
					proxy.$modal.msgSuccess('修改成功');
					open.value = false;
					getList();
				});
			} else {
				addLibrary(form.value).then(response => {
					proxy.$modal.msgSuccess('新增成功');
					open.value = false;
					getList();
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row: any) {
	const idList = row.id || ids.value;
	const showNames = row.productlibraryName || names.value;
	proxy.$modal.confirm('是否确认删除产品库名称为"' + showNames + '"的数据项？').then(function () {
		return delLibrary(idList);
	}).then(() => {
		getList();
		proxy.$modal.msgSuccess('删除成功');
	}).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download('manage/library/exportByStream', {
		...queryParams.value
	}, `library_${new Date().getTime()}.xlsx`);
}

getList();
</script>
